// bookinfo.js
var sliderWidth = 96;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    left: null,
    info: [],
    books: {},
    tabs: ["简介", "作者", "找相似"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0
  },

  preserve: function () {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    var that = this;
    var isbn = option.isbn;
    wx.request({
      url: 'https://55300249.qcloud.la/JapariFriends/searchBook?condition=isbn10=\'' + isbn + '\'',
      success: function (res) {
        that.setData({
          info: res.data[0]
        })
      }
    }),
      wx.request({
        url: 'https://55300249.qcloud.la/JapariFriends/left',
        data: {
          isbn10: isbn,
          existed: 1
        },
        success: function (res) {
          that.setData({
            left: res.data
          })
        }
      })

    //navbar
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
  },
  tabClick: function (e) {
    var that = this;
    var kind=this.data.info.kind

    console.log(kind)
    if (e.currentTarget.id == 2 && that.data.books.length == undefined) {
      wx.request({
        url: 'https://55300249.qcloud.la/JapariFriends/searchBook',
        data: {
          condition: 'kind=\'' + kind+'\''
        },
        method: 'get',
        success: function (res) {
          that.setData({
            books: res.data
          })
          console.log(res)
        }
      })
    }

    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },

  preserve: function () {
    var isbn = this.data.info.isbn10
    console.log(isbn)
    var ID = wx.getStorageSync('userID')
    wx.navigateTo({
      url: '../reserveform/reserveform?isbn=' + isbn,
    })
  }
})